/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React, { useEffect, useState } from 'react';
import { PageContainer } from '@ant-design/pro-layout';
import {
  cancelOrder,
  reallyReject,
  refuseRefunds,
  getOrderDetail,
  refundMoneyAndStatus,
} from '@/services/tools/orderManage';
import { Row, Col, Button, message, Space, Popconfirm } from 'antd';
// import KboutletsModal from '@/components/_pages/tools/orderManage/outletsModal';
import KbList from '@/components/_pages/tools/orderManage/DetailList';
import { orderSource, addresseeSource } from '@/components/_pages/tools/orderManage/index.config';

const Index = ({
  orderId,
  visible,
  drawerCallBack,
}: {
  orderId: string;
  visible: boolean;
  drawerCallBack: Function;
}) => {
  const [orderData, setOrderData] = useState<any>({});
  const [operation, setOperation] = useState<any>([]);
  const [options, setOptions] = useState<any>({});

  const getOrderDetails = () => {
    return getOrderDetail(orderId).then(({ code, data, msg }) => {
      if (code == 0) {
        const { order, operation: operationData, info } = data;
        setOrderData(order);
        setOperation(operationData);
        const hasOptions = ['0', '1', '2'].includes(order.status);
        const refund = hasOptions && order.freight > 0 && order.receipts_under_custody > 0;
        const refuseRefund =
          refund &&
          ((info.handle_status && !['1', '3'].includes(info.handle_status)) ||
            (info.handle_status == '3' &&
              info.pay_status != '0' &&
              info.handle_message.includes('快递员')));
        setOptions({
          hasOptions,
          refund,
          refuseRefund,
        });
      } else {
        message.error(msg);
      }
    });
  };

  const handleReallyReject = () => {
    const { id } = orderData;
    reallyReject({ orderNo: id }).then(({ code, msg }) => {
      if (code == 0) {
        message.success(msg);
        drawerCallBack();
      } else {
        message.success(msg);
      }
    });
  };

  const handleCancelOrder = () => {
    const { id: order_id, brand } = orderData;
    cancelOrder({ order_id, brand }).then(({ code, msg }) => {
      if (code == 0) {
        message.success(msg);
        drawerCallBack();
      } else {
        message.error(msg);
      }
    });
  };

  const handleRefundMoneyAndStatus = () => {
    const { id: order_id } = orderData;
    refundMoneyAndStatus({ order_id }).then(({ code, msg }) => {
      if (code == 0) {
        message.success(msg);
        drawerCallBack();
      } else {
        message.error(msg);
      }
    });
  };

  const handleRefund = () => {
    const { id: order_id } = orderData;
    refuseRefunds({ order_id }).then(({ code, msg }) => {
      if (code == 0) {
        message.success(msg);
        drawerCallBack();
      } else {
        message.error(msg);
      }
    });
  };

  useEffect(() => {
    if (!visible) return;
    getOrderDetails();
  }, []);

  return (
    <PageContainer>
      <Space style={{ padding: 10 }} wrap={true}>
        {options.hasOptions ? (
          <>
            {/* <KboutletsModal item={orderData} getOrderDetails={getOrderDetails}>
              <Button type='primary'>分配网点</Button>
            </KboutletsModal> */}
            <Popconfirm
              title='拒绝后该笔订单将会关闭,若产生预收费用将原路退回确认关闭?'
              onConfirm={handleReallyReject}
            >
              <Button type='primary'>拒绝</Button>
            </Popconfirm>
            <Popconfirm
              title='取消该笔订单，订单将不会受理是否取消？'
              onConfirm={handleCancelOrder}
            >
              <Button type='primary'>取消订单</Button>
            </Popconfirm>
            {options.refund ? (
              <Popconfirm title='你确定退款吗?' onConfirm={handleRefundMoneyAndStatus}>
                <Button type='primary'>退款</Button>
              </Popconfirm>
            ) : null}
            {options.refuseRefund ? (
              <Popconfirm title='你确定拒绝退款吗?' onConfirm={handleRefund}>
                <Button type='primary'>拒绝退款</Button>
              </Popconfirm>
            ) : null}
          </>
        ) : null}
      </Space>
      <Row style={{ width: '100%' }}>
        <Col xs={24} md={12} lg={6} xl={6} xxl={6}>
          <KbList data={orderData} options={orderSource} title='订单详情表' />
        </Col>
        <Col xs={24} md={12} lg={6} xl={6} xxl={6}>
          <KbList data={orderData} options={addresseeSource} title='收件人地址' />
        </Col>
        <Col xs={24} md={14} lg={8} xl={8} xxl={8}>
          <KbList data={operation} options={[]} title='操作列表' array />
        </Col>
      </Row>
    </PageContainer>
  );
};

export default Index;
